package com.zyx.maildemo.config;
import java.util.LinkedHashMap;
import	java.util.Map;

import com.zyx.maildemo.config.jwt.JWTFilter;
import com.zyx.maildemo.config.jwt.JWTRealm;
import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.apache.shiro.web.mgt.WebSecurityManager;
import org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.servlet.Filter;

/**
 * @ClassName: ShiroConfig
 * @author: zyx
 * @E-mail: 1377631190@qq.com
 * @DATE: 2019/7/27 14:19
 */
@Configuration
public class ShiroConfig {

    @Bean
    public JWTRealm jwtRealm() {
        return new JWTRealm();
    }

    @Bean
    public DefaultWebSecurityManager securityManager(@Autowired JWTRealm jwtRealm) {
        DefaultWebSecurityManager manager = new DefaultWebSecurityManager();
        manager.setRememberMeManager(null);
        manager.setCacheManager(null);
        manager.setRealm(jwtRealm);
        return manager;
    }

    /**
     * 配置shiro过滤器工厂
     * @param securityManager 安全管理器
     * @return
     */
    @Bean
    public ShiroFilterFactoryBean shiroFilterFactoryBean(@Autowired DefaultWebSecurityManager securityManager) {
        ShiroFilterFactoryBean filterFactory = new ShiroFilterFactoryBean();
        filterFactory.setSuccessUrl("/success");
        filterFactory.setLoginUrl("/login");
        filterFactory.setUnauthorizedUrl("/unauth");
        filterFactory.setSecurityManager(securityManager);
        
        Map<String, Filter> filterMap = new LinkedHashMap<>();
        filterMap.put("jwt", new JWTFilter());
        filterFactory.setFilters(filterMap);
        
        Map<String, String> maps = new LinkedHashMap<>();
        maps.put("/login", "anon");
        maps.put("/user/login", "anon");
        maps.put("/user/register", "anon");
        maps.put("/success", "anon");
        maps.put("/unauth", "anon");
        
        maps.put("/**", "jwt");
        
        filterFactory.setFilterChainDefinitionMap(maps);
        return filterFactory;
    }

    /**
     * 开启aop支持,为下面打开shiro注解做服务
     */
    @Bean
    public DefaultAdvisorAutoProxyCreator getDefaultAdvisorAutoProxyCreator() {
        DefaultAdvisorAutoProxyCreator proxyCreator = new DefaultAdvisorAutoProxyCreator();
        proxyCreator.setProxyTargetClass(true);
        return proxyCreator;
    }

    /**
     * 打开shiro注解支持
     * @param manager 安全管理器
     */
    @Bean
    public AuthorizationAttributeSourceAdvisor getAuthorizationAttributeSourceAdvisor(WebSecurityManager manager) {
        AuthorizationAttributeSourceAdvisor advisor = new AuthorizationAttributeSourceAdvisor();
        advisor.setSecurityManager(manager);
        return advisor;
    }

}
